package com.yanqu.road.server.cmd.mall;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.mall.MallProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

import static com.yanqu.road.server.manger.config.MallMgr.getRandomMallItemWeigh;

@Cmd(code = Protocol.S_MALL_RANDOM_WEIGHT_LIST, desc = "商店随机商品信息(权重)")
public class MallRandomWeightCmd extends AbstractCommand {

    int shopType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MallProto.MallRandomWeightReqMsg reqMsg = MallProto.MallRandomWeightReqMsg.parseFrom(packet.getBytes());
        shopType = reqMsg.getShopType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(shopType <= 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        MallProto.MallRandomWeightRespMsg.Builder respMsg = MallProto.MallRandomWeightRespMsg.newBuilder();
        Map<Integer, Integer> weighMap = getRandomMallItemWeigh(shopType);
        for (Map.Entry<Integer, Integer> entry : weighMap.entrySet()) {
            MallProto.MallRandomWeightTemp.Builder builder = MallProto.MallRandomWeightTemp.newBuilder();
            builder.setGoodId(entry.getKey());
            builder.setWeight(entry.getValue());
            respMsg.addWeight(builder);
        }
        respMsg.setRet(0);
        player.sendPacket(Protocol.U_MALL_RANDOM_WEIGHT_LIST, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_MALL_RANDOM_WEIGHT_LIST, MallProto.MallRandomWeightRespMsg.newBuilder().setRet(ret));
        }
    }
}
